package com.koron.car.web.applycar.service.impl;

import com.koron.car.config.Constants;
import com.koron.car.web.applycar.bean.entity.CarEntourage;
import com.koron.car.web.applycar.mapper.CarEntourageMapper;
import com.koron.car.web.applycar.service.CarEntourageService;
import com.koron.car.web.common.CommonUtils;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (CarEntourage)表服务实现类
 *
 * @author twcao
 * @since 2022-03-09 10:37:13
 */
@Service
public class CarEntourageServiceImpl implements CarEntourageService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public CarEntourage queryById(SessionFactory factory, String id) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param entourage 查询起条件
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<CarEntourage> queryList(SessionFactory factory, CarEntourage entourage) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        return mapper.queryList(entourage);
    }

    /**
     * 新增数据
     *
     * @param entourage 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public CarEntourage insert(SessionFactory factory, CarEntourage entourage) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        entourage.setLeader(entourage.getLeader() == null ? Constants.YesOrNo.NO : Constants.YesOrNo.YES);
        mapper.insert(entourage);
        return entourage;
    }

    /**
     * 新增数据
     *
     * @param factory
     * @param entourages 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<CarEntourage> entourages) {
        if(CollectionUtils.isEmpty(entourages)) {
            return 0;
        }
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        CommonUtils.sorted(entourages);
        entourages.stream().forEach(row -> {
            row.setId(CodeTools.getCode32());
            row.setLeader(row.getLeader() == null ? Constants.YesOrNo.NO : row.getLeader());
        });
        mapper.insertBatch(entourages);
        return entourages.size();
    }

    /**
     * 修改数据
     *
     * @param entourage 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public CarEntourage update(SessionFactory factory, CarEntourage entourage) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        mapper.update(entourage);
        return this.queryById(factory, entourage.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        return mapper.deleteById(id) > 0;
    }

    @Override
    @TaskAnnotation("deleteByApplyId")
    public boolean deleteByApplyId(SessionFactory factory, String applyId) {
        CarEntourageMapper mapper = factory.getMapper(CarEntourageMapper.class);
        return mapper.deleteByApplyId(applyId) > 0;
    }
}
